package com.ys.controller.api.sys;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.sys.YsModalSelectorField;
import com.ys.enume.sys.YsModalSelectorFieldEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.YsModalSelectorFieldService;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

@Controller
@RequestMapping("/api/inter/ysModalSelectorField")
public class ApiInterYsModalSelectorFieldController {
	
//	private static final Logger logger = Logger.getLogger(ApiAdminLoginController.class);
	
	@Autowired
	private CommonApiService<YsModalSelectorField> commonApiService;
	@Autowired
	private YsModalSelectorFieldService ysModalSelectorFieldService;

//	@RequiresPermissions("ysModalSelector:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute YsModalSelectorField cond
			){
		cond.setOrderby("A.SortNo asc");
		
		return commonApiService.searchLay(cond);
	}
	
	@RequestMapping("/dtree")
    @ResponseBody
	public DtreeSqlDto dtree(Model model, HttpServletRequest request, HttpServletResponse response
			){
		return ysModalSelectorFieldService.dtree();
	}
	
	/**
	 * 批量新增
	 */
	@PostMapping("/batchAdd")
	@ResponseBody
	public R batchAdd(HttpServletRequest request,
			String subData){
		
		JSONArray detailJA = JSON.parseArray(subData);
		for (int i = 0; i < detailJA.size(); i++) {
			JSONObject JO = detailJA.getJSONObject(i);
			Long pid = JO.getLong("pid");
			String code = JO.getString("code");
			String name = JO.getString("name");
			String type = YsModalSelectorFieldEnum.Type.TEXT.value;
			Integer sortNo = i + 1;
			Integer stateCondition = YsModalSelectorFieldEnum.StateCondition.NO.value;
			String conditionCode = null;
			String dictKeyBase = null;
			Integer align = YsModalSelectorFieldEnum.Align.CENTER.value;
			Integer width = 100;
			ysModalSelectorFieldService.add(pid, code, name, type, sortNo, stateCondition, conditionCode, dictKeyBase, align, width);
		}
		return R.ok();
	}
	
	/**
	 * 新增
	 */
	@PostMapping("/add")
    @ResponseBody
	public R add(Long pid,
			String code,
			String name,
			String type,
			Integer sortNo,
			Integer stateCondition,
			String conditionCode,
			String dictKeyBase,
			Integer align,
			Integer width){
		
		return ysModalSelectorFieldService.add(pid, code, name, type, sortNo, stateCondition, conditionCode, dictKeyBase, align, width);
	}
	
	/**
	 * 编辑
	 */
	@PostMapping("/edit")
    @ResponseBody
	public R edit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id, String name, String code, String type, Integer stateCondition, String conditionCode){
		
		return ysModalSelectorFieldService.edit(id, name, code, type, stateCondition, conditionCode);
	}
	
	/*
	 * 彻底删除
	 */
	@PostMapping("/deleteTrue")
	@ResponseBody
	public R deleteTrue(HttpServletRequest request, HttpServletResponse response,
			Long id) {
		return ysModalSelectorFieldService.delete(id);
	}
	
	/**
	 * 调整顺序
	 */
	@PostMapping("/resort")
    @ResponseBody
	public R resort(Model model, HttpServletRequest request, HttpServletResponse response,
			String rows){
		return ysModalSelectorFieldService.resort(rows);
	}
	
	/**
	 * 配置选项
	 */
	@PostMapping("/optionSetting")
    @ResponseBody
	public R optionSetting(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id, String dictKey){
		return ysModalSelectorFieldService.optionSetting(id, dictKey);
	}
	
}
